<?php
/**
* This file acts to keep the db scheme updated.  It should be run on the mysql backend db after code has been
* 	updated.  It is safe to run on existing data, as it will look at the current db storage scheme & update it
*	without removing existing data.
* You have to be logged in as news to run this file.  Users trying to access it without having that login will
*	be presented with an error.
*
* @package joiner
* @todo Enable functionality with Postgress, currently only works with mysql.
**/

// The current version of the joiner db scheme.
$joiner_dbversion = 1;

// Setup variables.
define("context" , "joiner_setup");
global $CFG,$db;
require("../../../includes.php");
require_once '../config.php';
require_once '../lib.php';
  
// See if we're using postgreesss or an old version of mysql.  Cancel, as this only works with MySQL.
if ($CFG->dbtype == 'postgres7') {
	error('This only works with a MySQL database');
	die();
}

if (!$rs = $db->Execute("SELECT version();")) {
	error('Error accessing MySQL database when trying to run SELECT version()');
	die();
}

// See if a user other than news is trying to run this.
if ($USER->username <> 'news') {
	error('You can only run this function if you logged on as "news"');
	die();
}

// Start upgrading.
if ( joiner_getVersion() < $joiner_dbversion ) {
	echo ( 'Your DB scheme does not match the most current version...  Beginning upgrade.');
	switch ( joiner_getVersion() ) {
			case -1:
			   gotoVersion1();
			   // no break, need to continue updating.

   }
	echo ( '<br/>Update finished<br/>' );
} else {

	echo ( 'Your DB scheme is up to date.  Exiting function.');
}

/**
* Get the current version, or -1 if the table isn't present
* @return Integer
**/
function joiner_getVersion() {
	global $CFG;
	
    // Find.
    $versions = recordset_to_array(
		get_recordset_sql("SELECT version as v, version FROM " . $CFG->prefix . "joiner_version")
		);
	
    if ( $versions ) {
		$i = -1;
        foreach ($versions as $version) {	
			if ($i < $version->version) {
				$i = $version->version;
			}
		}
		return $version->version;
	} else {
		// Table not found.  joiners probably aren't installed yet.
		return -1;
	}
}


/** 
* Update to version 1 -- first db schema
**/
function gotoVersion1() {
	global $CFG;	
	$prefix = $CFG->prefix;
	$db = $CFG->dbname;
			

/* ---------------------------------------------------------------
Tree

This contains nodes for navigating between objects.  

Probably needs some more indexes (along with the rest of the joiner
tables).

Not currently being used.  Will implement for the presentation module, and possibly 
the ability to import/link to other people's pages.
--------------------------------------------------------------- */
	execute_sql( 
			"CREATE TABLE  `{$db}`.`{$prefix}joiner_data` (
		  `user_ident` INTEGER UNSIGNED NOT NULL,
		  `type` VARCHAR(45) NOT NULL,
		  `data` VARCHAR(45) NOT NULL,
		  PRIMARY KEY(`user_ident`, `type`, `data`),
		  INDEX `user_ident`(`user_ident`)
			);"
			);

/* ---------------------------------------------------------------
Tracks what version of the db we're working with.
--------------------------------------------------------------- */
			
	execute_sql(
			"CREATE TABLE  `{$db}`.`{$prefix}joiner_version` (
			  `version` int(10) unsigned NOT NULL,
			  PRIMARY KEY  (`version`)
			);"
	);
			
// Update version to schema 1
	$version = new StdClass;
	$version->version = 1;
	insert_record("joiner_version",$version);

}
?>